#
# Filename: PvcsRemote.py
# $Id: PvcsRemote.py,v 1.1 1999/08/13 12:39:56 toni Exp $
#
# defines the real call functions for the Pvcs Remote interface
#
# Author: Uwe Stieber
# Copyright (c) 1998, 1999 TakeFive Software, Salzburg
#

# import the necessary modules
import os
import sys
import string
import PVCSConstants
import vcs

# some global variables
from vcs import TEST

# remote command prefix
RemoteHost = "dummy"
if os.environ.has_key("PVCS_REMOTE_HOST"):
	RemoteHost = os.environ["PVCS_REMOTE_HOST"]
if not os.environ.has_key("SNiFF_CMVC_REMOTE_USER"):
	RemoteCommandPrefix = "%s\\System32\\rsh.exe %s " % (os.environ["SystemRoot"], RemoteHost)
else:
	RemoteCommandPrefix = "%s\\System32\\rsh.exe %s -l %s " % (os.environ["SystemRoot"], RemoteHost, os.environ["SNiFF_CMVC_REMOTE_USER"])

# remote pvcs path mapping
PathMap = {
			"G:":	"/Users/uwe"
		}

def translatePathnames(aString):
	convertedString = aString
	if TEST:
		print "(@B@I%s@I@B)@B@g>>>@x translatePathnames" % __name__
	for aPathToConvert in PathMap.keys():
		if TEST:
			print "(@B@I%s@I@B)\t%s --> %s" % (__name__, aPathToConvert, PathMap[aPathToConvert])
		convertedString = string.replace(convertedString, aPathToConvert, PathMap[aPathToConvert])
	if TEST:
		print "(@B@I%s@I@B)@B@r<<<@x translatePathnames" % __name__
	return convertedString

# call the put command
def put(*arguments):
	if TEST:
		print "(@B@I%s@I@B)@B@g>>>@x put" % __name__
	argumentString = ""
	# convert argument tuple to string
	for seq in arguments:
		for aArgument in seq:
			argumentString = "%s %s" % (argumentString, string.replace(aArgument, r'"', r"\""))
	# translate Pathnames
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
	argumentString = translatePathnames(argumentString)
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
		print "(@B@I%s@I@B)\texecute command: %s put %s" % (__name__, RemoteCommandPrefix, argumentString)
	try:
		rc, _out = vcs.syscmd("%s ''put %s''" % (RemoteCommandPrefix, argumentString))
	except:
		import traceback
		traceback.print_exc()
	if TEST:
		print "(@B@I%s@I@B)\trc = %d" % (__name__, rc)
		print "(@B@I%s@I@B)\t_out = %s" % (__name__, _out)
	if TEST:
		print "(@B@I%s@I@B)@B@r<<<@x put (%d)" % (__name__, rc)
	return rc

# call the get command
def get(*arguments):
	if TEST:
		print "(@B@I%s@I@B)@B@g>>>@x get" % __name__
	argumentString = ""
	# convert argument tuple to string
	for seq in arguments:
		for aArgument in seq:
			argumentString = "%s %s" % (argumentString, string.replace(aArgument, r'"', r"\""))
	# translate Pathnames
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
	argumentString = translatePathnames(argumentString)
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
		print "(@B@I%s@I@B)\texecute command: %s get %s" % (__name__, RemoteCommandPrefix, argumentString)
	try:
		rc, _out = vcs.syscmd("%s ''get %s''" % (RemoteCommandPrefix, argumentString))
	except:
		import traceback
		traceback.print_exc()
	if TEST:
		print "(@B@I%s@I@B)\trc = %d" % (__name__, rc)
		print "(@B@I%s@I@B)\t_out = %s" % (__name__, _out)
	if TEST:
		print "(@B@I%s@I@B)@B@r<<<@x get (%d)" % (__name__, rc)
	return rc

# call the vcs_command command
def vcs_command(*arguments):
	if TEST:
		print "(@B@I%s@I@B)@B@g>>>@x vcs_command" % __name__
	argumentString = ""
	# convert argument tuple to string
	for seq in arguments:
		for aArgument in seq:
			argumentString = "%s %s" % (argumentString, string.replace(aArgument, r'"', r"\""))
	# translate Pathnames
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
	argumentString = translatePathnames(argumentString)
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
		print "(@B@I%s@I@B)\texecute command: %s vcs %s" % (__name__, RemoteCommandPrefix, argumentString)
	try:
		rc, _out = vcs.syscmd("%s ''vcs %s''" % (RemoteCommandPrefix, argumentString))
	except:
		import traceback
		traceback.print_exc()
	if TEST:
		print "(@B@I%s@I@B)\trc = %d" % (__name__, rc)
		print "(@B@I%s@I@B)\t_out = %s" % (__name__, _out)
	if TEST:
		print "(@B@I%s@I@B)@B@r<<<@x vcs_command (%d)" % (__name__, rc)
	return rc

# call some sh command and grep the output
def sh_getoutput(*arguments):
	if TEST:
		print "(@B@I%s@I@B)@B@g>>>@x sh_getoutput" % __name__
	argumentString = ""
	# convert argument tuple to string
	for seq in arguments:
		for aArgument in seq:
			argumentString = "%s %s" % (argumentString, string.replace(aArgument, r'"', r"\""))
	# translate Pathnames
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
	argumentString = translatePathnames(argumentString)
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
		print "(@B@I%s@I@B)\texecute command: %s %s" % (__name__, RemoteCommandPrefix, argumentString)
	try:
		rc, _out = vcs.syscmd("%s ''$SHELL -c %s''" % (RemoteCommandPrefix, argumentString))
	except:
		import traceback
		traceback.print_exc()
	if TEST:
		print "(@B@I%s@I@B)\trc = %d" % (__name__, rc)
		print "(@B@I%s@I@B)\t_out = %s" % (__name__, _out)
	if TEST:
		print "(@B@I%s@I@B)@B@r<<<@x sh_getoutput" % __name__
	return _out

# call the vlog_command command
def vlog_command(*arguments):
	if TEST:
		print "(@B@I%s@I@B)@B@g>>>@x vlog_command" % __name__
	argumentString = ""
	# convert argument tuple to string
	for seq in arguments:
		for aArgument in seq:
			argumentString = "%s %s" % (argumentString, string.replace(aArgument, r'"', r"\""))
	# translate Pathnames
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
	argumentString = translatePathnames(argumentString)
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
		print "(@B@I%s@I@B)\texecute command: %s vlog %s" % (__name__, RemoteCommandPrefix, argumentString)
	try:
		rc, _out = vcs.syscmd("%s ''vlog %s''" % (RemoteCommandPrefix, argumentString))
	except:
		import traceback
		traceback.print_exc()
	if TEST:
		print "(@B@I%s@I@B)\trc = %d" % (__name__, rc)
		print "(@B@I%s@I@B)\t_out = %s" % (__name__, _out)
	if TEST:
		print "(@B@I%s@I@B)@B@r<<<@x vlog_command" % __name__
	return _out

# call the vlog_command command and returns a StringStream
def vlog_popen(*arguments):
	if TEST:
		print "(@B@I%s@I@B)@B@g>>>@x vlog_popen" % __name__
	cmd = ""
	file = ""
	# convert argument tuple to string
	seq = arguments[0]
	cmd = translatePathnames(string.replace(seq[0], r'"', r"\""))
	file = seq[1]
	if file:
		file = string.join(map(vcs.normpath, string.split(file)))
	file = translatePathnames(file)
	if TEST:
		print "(@B@I%s@I@B)\texecute command: %s %s %s" % (__name__, RemoteCommandPrefix, cmd, file)
	try:
		rc, _out = vcs.syscmd("%s ''%s %s''" % (RemoteCommandPrefix, cmd, file))
	except:
		import traceback
		traceback.print_exc()
	if TEST:
		print "(@B@I%s@I@B)\trc = %d" % (__name__, rc)
		print "(@B@I%s@I@B)\t_out = %s" % (__name__, _out)
	if TEST:
		print "(@B@I%s@I@B)@B@r<<<@x vlog_popen" % __name__
	return vcs.StringStream(_out)

# call the vdel command
def vdel(*arguments):
	if TEST:
		print "(@B@I%s@I@B)@B@g>>>@x vdel" % __name__
	argumentString = ""
	# convert argument tuple to string
	for seq in arguments:
		for aArgument in seq:
			argumentString = "%s %s" % (argumentString, string.replace(aArgument, r'"', r"\""))
	# translate Pathnames
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
	argumentString = translatePathnames(argumentString)
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
		print "(@B@I%s@I@B)\texecute command: %s vdel %s" % (__name__, RemoteCommandPrefix, argumentString)
	try:
		rc, _out = vcs.syscmd("%s ''vdel %s''" % (RemoteCommandPrefix, argumentString))
	except:
		import traceback
		traceback.print_exc()
	if TEST:
		print "(@B@I%s@I@B)\trc = %d" % (__name__, rc)
		print "(@B@I%s@I@B)\t_out = %s" % (__name__, _out)
	if TEST:
		print "(@B@I%s@I@B)@B@r<<<@x vdel (%d)" % (__name__, rc)
	return rc

# call the vdiff command
def vdiff(*arguments):
	if TEST:
		print "(@B@I%s@I@B)@B@g>>>@x vdiff" % __name__
	argumentString = ""
	# convert argument tuple to string
	for seq in arguments:
		for aArgument in seq:
			argumentString = "%s %s" % (argumentString, string.replace(aArgument, r'"', r"\""))
	# translate Pathnames
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
	argumentString = translatePathnames(argumentString)
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
		print "(@B@I%s@I@B)\texecute command: %s vdiff %s" % (__name__, RemoteCommandPrefix, argumentString)
	try:
		rc, _out = vcs.syscmd("%s ''vdiff %s''" % (RemoteCommandPrefix, argumentString))
	except:
		import traceback
		traceback.print_exc()
	if TEST:
		print "(@B@I%s@I@B)\trc = %d" % (__name__, rc)
		print "(@B@I%s@I@B)\t_out = %s" % (__name__, _out)
	if TEST:
		print "(@B@I%s@I@B)@B@r<<<@x vdiff (%d)" % (__name__, rc)
	return rc

# call the diff command
def diff(*arguments):
	if TEST:
		print "(@B@I%s@I@B)@B@g>>>@x diff" % __name__
	argumentString = ""
	# convert argument tuple to string
	for seq in arguments:
		for aArgument in seq:
			argumentString = "%s %s" % (argumentString, string.replace(aArgument, r'"', r"\""))
	# translate Pathnames
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
	argumentString = translatePathnames(argumentString)
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
		print "(@B@I%s@I@B)\texecute command: %s diff %s" % (__name__, RemoteCommandPrefix, argumentString)
	try:
		rc, _out = vcs.syscmd("%s ''diff %s''" % (RemoteCommandPrefix, argumentString))
	except:
		import traceback
		traceback.print_exc()
	if TEST:
		print "(@B@I%s@I@B)\trc = %d" % (__name__, rc)
		print "(@B@I%s@I@B)\t_out = %s" % (__name__, _out)
	if TEST:
		print "(@B@I%s@I@B)@B@r<<<@x diff" % __name__
	return _out

# call the rm -f command
def rm_f(*arguments):
	if TEST:
		print "(@B@I%s@I@B)@B@g>>>@x rm_f" % __name__
	argumentString = ""
	# convert argument tuple to string
	for seq in arguments:
		for aArgument in seq:
			argumentString = "%s %s" % (argumentString, string.replace(aArgument, r'"', r"\""))
	# translate Pathnames
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
	argumentString = translatePathnames(argumentString)
	if TEST:
		print "(@B@I%s@I@B)\targumentString = %s" % (__name__, argumentString)
		print "(@B@I%s@I@B)\texecute command: %s put %s" % (__name__, RemoteCommandPrefix, argumentString)
	try:
		rc, _out = vcs.syscmd("%s ''rm -f %s''" % (RemoteCommandPrefix, argumentString))
	except:
		import traceback
		traceback.print_exc()
	if TEST:
		print "(@B@I%s@I@B)\trc = %d" % (__name__, rc)
		print "(@B@I%s@I@B)\t_out = %s" % (__name__, _out)
	if TEST:
		print "(@B@I%s@I@B)@B@r<<<@x rm_f (%d)" % (__name__, rc)
	return rc
